package com.example.pkmedical.Activity.rollovershock;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextClock;
import android.widget.TextView;

import com.example.pkmedical.APP.MyApp;
import com.example.pkmedical.Base.BaseActivity;
import com.example.pkmedical.Event.MessageWrap;
import com.example.pkmedical.MainActivity;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.CommandUtil;
import com.example.pkmedical.Utils.DistinguishUtil;
import com.example.pkmedical.Utils.FinishActivityManager;
import com.example.pkmedical.Utils.IoTimer;
import com.example.pkmedical.Utils.SPUtils;
import com.example.pkmedical.Utils.SerialProtUtil;
import com.example.pkmedical.Utils.TestTime;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public class RolloverShockNextActivity extends BaseActivity {
    private static final String TAG = "yangshuo";
    @BindView(R.id.main_house)
    ImageView mainHouse;
    @BindView(R.id.main_house_text)
    TextView mainHouseText;
    @BindView(R.id.main_date)
    TextClock mainDate;
    @BindView(R.id.rollovershock_stop_button)
    Button rollovershockStopButton;
    @BindView(R.id.rollovershock_time_num)
    TextView rollovershockTimeNum;
    @BindView(R.id.rollovershock_left_img)
    ImageView rollovershockLeftImg;
    @BindView(R.id.rollovershock_left_text)
    TextView rollovershockLeftText;
    @BindView(R.id.rollovershock_left_num_text)
    TextView rollovershockLeftNumText;
    @BindView(R.id.rollovershock_left_num)
    TextView rollovershockLeftNum;
    @BindView(R.id.rollovershock_left_layout)
    RelativeLayout rollovershockLeftLayout;
    @BindView(R.id.rollovershock_ping_img)
    ImageView rollovershockPingImg;
    @BindView(R.id.rollovershock_ping_text)
    TextView rollovershockPingText;
    @BindView(R.id.rollovershock_ping_layout)
    RelativeLayout rollovershockPingLayout;
    @BindView(R.id.rollovershock_right_img)
    ImageView rollovershockRightImg;
    @BindView(R.id.rollovershock_right_text)
    TextView rollovershockRightText;
    @BindView(R.id.rollovershock_right_num_text)
    TextView rollovershockRightNumText;
    @BindView(R.id.rollovershock_right_num)
    TextView rollovershockRightNum;
    @BindView(R.id.rollovershock_right_layout)
    RelativeLayout rollovershockRightLayout;
    @BindView(R.id.rollovershock_right_time_num)
    TextView rollovershockRightTimeNum;
    @BindView(R.id.rollovershock_qiaoji_img)
    ImageView rollovershockQiaojiImg;
    @BindView(R.id.rollovershock_qiaoji_textone)
    TextView rollovershockQiaojiTextone;
    @BindView(R.id.rollovershock_qiaoji_texttwo)
    TextView rollovershockQiaojiTexttwo;
    @BindView(R.id.rollovershock_qiaoji_textthree)
    TextView rollovershockQiaojiTextthree;
    @BindView(R.id.rollovershock_qiaoji_textfour)
    TextView rollovershockQiaojiTextfour;
    @BindView(R.id.rollovershock_qiaoji_textfive)
    TextView rollovershockQiaojiTextfive;
    @BindView(R.id.rollovershock_right_two_layout)
    RelativeLayout rollovershockRightTwoLayout;
    @BindView(R.id.rollovershock_zhendong_img)
    ImageView rollovershockZhendongImg;
    @BindView(R.id.rollovershock_zhendong_textone)
    TextView rollovershockZhendongTextone;
    @BindView(R.id.rollovershock_zhendong_texttwo)
    TextView rollovershockZhendongTexttwo;
    @BindView(R.id.rollovershock_zhendong_textthree)
    TextView rollovershockZhendongTextthree;
    @BindView(R.id.rollovershock_zhendong_textfour)
    TextView rollovershockZhendongTextfour;
    @BindView(R.id.rollovershock_zhendong_textfive)
    TextView rollovershockZhendongTextfive;
    @BindView(R.id.rollovershock_right_three_layout)
    RelativeLayout rollovershockRightThreeLayout;
    @BindView(R.id.rollovershock_cefan_texttwo)
    TextView rollovershockCefanTexttwo;
    @BindView(R.id.rollovershock_qiaoji_textsix)
    TextView rollovershockQiaojiTextsix;
    @BindView(R.id.rollovershock_zhendong_textsix)
    TextView rollovershockZhendongTextsix;
    @BindView(R.id.rollovershock_qiaoji_qiangdu)
    TextView rollovershockQiaojiQiangdu;
    @BindView(R.id.rollovershock_zhendong_qiangdu)
    TextView rollovershockZhendongQiangdu;

    private int mType;//getIntent获取上个界面传过来的类型
    private int mRestType = 1;//侧翻的档位状态，1：整装，2：一般，3：最低
    private byte[] mBytes;
    private boolean isCFTimer = false;
    private boolean isQZTimer = false;
    //定时相关
    private Handler stepTimeHandler;
    private Runnable mTicker;
    long startTime = 0;
    //循环时间（毫秒）
    long loopTime = 0;
    //当前侧翻状态 1：右侧翻，2：平卧，3：左侧翻
    private int mRolloverNowType = 1;
    //当前敲击震动状态 1：敲击，2：震动
    private int mShockNowType = 1;
    private long mRolloverNowTime = SPUtils.getInstance().getInt("rollover_right_minute") * 60000;
    private long mShockNowTime = SPUtils.getInstance().getInt("shock_time_qiaoji") * 60000;
    private boolean isCFTrue = false;//侧翻定制是否执行中
    private boolean isQJCFTrue = false;//敲击震动侧翻定制是否执行中
    CountDownTimer countDownTimer;
    private boolean needFinish = false;
    //当前进入平卧是什么状态;true:one,false:two
    private boolean nowCFtype = true;
    //当前执行状态：0:定制敲击+震动，1：定制只敲击，2：定制只震动 3：整装敲击震动 4：中等+最低敲击震动
    private int whatRunning = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rollovershock);
        ButterKnife.bind(this);
        FinishActivityManager.getManager().addActivity(this);
    }

    @Override
    public void initData() {
        mType = getIntent().getIntExtra("rollovershock_type", 1);
        CommandUtil.TYPR_QJCF = mType;
        switch (mType) {
            case 1:
                loopTime = CommandUtil.THREE_MINUTE;
                rollovershockLeftNum.setText("100%");
                rollovershockRightNum.setText("100%");
                LoopFixedTimer(loopTime, 100000, mType);
                break;
            case 2:
                loopTime = CommandUtil.THREE_MINUTE;
                rollovershockLeftNum.setText("80%");
                rollovershockRightNum.setText("80%");
                LoopFixedTimer(loopTime, 100000, mType);
                break;
            case 3:
                loopTime = CommandUtil.THREE_MINUTE;
                rollovershockLeftNum.setText("60%");
                rollovershockRightNum.setText("60%");
                LoopFixedTimer(loopTime, 100000, mType);
                break;
            case 4:
                rollovershockLeftNum.setText(SPUtils.getInstance().getInt("rollover_left_bfb") * 10 + "%");
                rollovershockRightNum.setText(SPUtils.getInstance().getInt("rollover_right_bfb") * 10 + "%");
                Log.d("yangshuoleftTime", SPUtils.getInstance().getInt("rollover_left_minute") + "");
                Log.d("yangshuoRightTime", SPUtils.getInstance().getInt("rollover_right_minute") + "");
                Log.d("yangshuoPingTime", SPUtils.getInstance().getInt("rollover_ping_minute") + "");
                Log.d("yangshuoleftBFB", SPUtils.getInstance().getInt("rollover_left_bfb") + "%");
                Log.d("yangshuoRightBFB", SPUtils.getInstance().getInt("rollover_right_bfb") + "%");
                rightRolloverMth();
                //每隔一秒钟检测一次
                IoTimer.shareTimer().scheduleTimer(1000, new Runnable() {
                    @Override
                    public void run() {
                        if (!isCFTrue) {
                            isCFTrue = true;
                            mRolloverTime();
                        }
                    }
                }, 1000000, 0);
                break;
            case 5:
                whatRunning = 3;
                //敲击整装
                rollovershockZhendongQiangdu.setText(getString(R.string.shock_qiangdu) + ":" + getString(R.string.shock_gao));
                rollovershockQiaojiQiangdu.setText(getString(R.string.shock_qiangdu) + ":" + getString(R.string.shock_gao));
                QJCF(CommandUtil.FIFTEEN_MINUTE, "5.0bps", "15.0bps", "15", "15", 1);
                break;
            case 6:
                whatRunning = 4;
                //敲击中等
                rollovershockZhendongQiangdu.setText(getString(R.string.shock_qiangdu) + ":" + getString(R.string.shock_zhong));
                rollovershockQiaojiQiangdu.setText(getString(R.string.shock_qiangdu) + ":" + getString(R.string.shock_zhong));
                QJCF(CommandUtil.TEN_MINUTE, "5.0bps", "13.0bps", "10", "10", 2);
                break;
            case 7:
                whatRunning = 4;
                //敲击最低
                rollovershockZhendongQiangdu.setText(getString(R.string.shock_qiangdu) + ":" + getString(R.string.shock_di));
                rollovershockQiaojiQiangdu.setText(getString(R.string.shock_qiangdu) + ":" + getString(R.string.shock_di));
                QJCF(CommandUtil.TEN_MINUTE, "4.0bps", "10.0bps", "10", "10", 3);
                break;
            case 8:
                //敲击定制
                switch (CommandUtil.SHOCK_QIAOJI_STRENGTH) {
                    case 1:
                        rollovershockQiaojiQiangdu.setText(getString(R.string.shock_qiangdu) + ":" + getString(R.string.remind_off));
                        break;
                    case 2:
                        rollovershockQiaojiQiangdu.setText(getString(R.string.shock_qiangdu) + ":" + getString(R.string.shock_di));
                        break;
                    case 3:
                        rollovershockQiaojiQiangdu.setText(getString(R.string.shock_qiangdu) + ":" + getString(R.string.shock_zhong));
                        break;
                    case 4:
                        rollovershockQiaojiQiangdu.setText(getString(R.string.shock_qiangdu) + ":" + getString(R.string.shock_gao));
                        break;
                }
                switch (CommandUtil.SHOCK_ZHENDONG_STRENGTH) {
                    case 1:
                        rollovershockZhendongQiangdu.setText(getString(R.string.shock_qiangdu) + ":" + getString(R.string.remind_off));
                        break;
                    case 2:
                        rollovershockZhendongQiangdu.setText(getString(R.string.shock_qiangdu) + ":" + getString(R.string.shock_di));
                        break;
                    case 3:
                        rollovershockZhendongQiangdu.setText(getString(R.string.shock_qiangdu) + ":" + getString(R.string.shock_zhong));
                        break;
                    case 4:
                        rollovershockZhendongQiangdu.setText(getString(R.string.shock_qiangdu) + ":" + getString(R.string.shock_gao));
                        break;
                }
                QJCF(CommandUtil.TEN_MINUTE, CommandUtil.SHOCK_QIAOJI_BPS + "bps", CommandUtil.SHOCK_ZHENDONG_BPS + "bps",
                        SPUtils.getInstance().getInt("shock_time_qiaoji") + "",
                        SPUtils.getInstance().getInt("shock_time_zhendong") + "", 4);
                break;
        }
        mTimer();
    }

    @Override
    public void initView() {
        mainHouse.setImageResource(R.drawable.img_title_fbzl);
        mainHouseText.setText(R.string.title_fbzl);
        all_off();
        QZAllOff();
        SerialProtUtil.getInstance().sendBytes300(2, CommandUtil.OUT_ZDCQ);
        SerialProtUtil.getInstance().sendBytes400(2, CommandUtil.OUT_HELP);

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }

    @Override
    public void onReceiveEvent(MessageWrap event) {
        super.onReceiveEvent(event);
        if (event.getCode() == MessageWrap.ROLLOVERIMG) {
            Log.d(TAG, "onDataReceivedRollovering [ byte[] ]: " + Arrays.toString(event.getBytes()));
            mBytes = event.getBytes();
            distinguish(mBytes);
        } else {
            Log.d(TAG, "onDataReceivedRollovering [ byte[] ]: NoThisData");
        }
    }

    /**
     * 返回数据识别
     */
    private void distinguish(byte[] bytes) {

        if (bytes[3] == 11 && bytes[4] == 1 && needFinish) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    IoTimer.destroyTimer();
                    if (countDownTimer != null) {
                        countDownTimer.cancel();
                    }
                    isCFTimer = false;
                    isQZTimer = false;
                    SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.CHANGGUI_TTYPE);
                    RolloverShockNextActivity.this.finish();
                }
            });
            return;
        }
        if (DistinguishUtil.distinguishUtil(bytes)) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    switch (bytes[5]) {
                        case 0x01:
                            all_off();
                            RightOn();
                            break;
                        case 0x02:
                            all_off();
                            pingOn();
                            break;
                        case 0x03:
                            all_off();
                            leftOn();
                            break;
                        //敲击震动
                        case 0x04:
                            QZAllOff();
                            Log.d("yangshuo", "QIAOJIONNNNNNNNNNNNN");
                            QiaojiOn();
                            if (whatRunning == 1) {
                                TimeCount();
                            }
                            break;
                        case 0x05:
                            QZAllOff();
                            Log.d("yangshuo", "ZHENDONGONNNNNNNNNNNNN");
                            ZhendongOn();
                            TimeCount();
                            break;
                    }
                }
            });
        } else {
            Log.d("yangshuo", "000000000000000000000000");
        }
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void showDialog(String... msg) {

    }

    @OnClick(R.id.rollovershock_stop_button)
    public void onViewClicked() {


    }

    private void all_off() {
        rollovershockRightImg.setImageResource(R.drawable.rollovershock_left_off);

        rollovershockLeftText.setTextColor(getResources().getColor(R.color.white));
        rollovershockLeftNumText.setTextColor(getResources().getColor(R.color.white));
        rollovershockLeftNum.setTextColor(getResources().getColor(R.color.white));
        rollovershockLeftLayout.setBackgroundResource(R.drawable.rollovershock_off_item);

        rollovershockPingImg.setImageResource(R.drawable.rollovershock_ping_off);
        rollovershockPingText.setTextColor(getResources().getColor(R.color.white));
        rollovershockPingLayout.setBackgroundResource(R.drawable.rollovershock_off_item);

        rollovershockLeftImg.setImageResource(R.drawable.rollovershock_right_off);
        rollovershockRightText.setTextColor(getResources().getColor(R.color.white));
        rollovershockRightNumText.setTextColor(getResources().getColor(R.color.white));
        rollovershockRightNum.setTextColor(getResources().getColor(R.color.white));
        rollovershockRightLayout.setBackgroundResource(R.drawable.rollovershock_off_item);
    }

    private void leftOn() {

        rollovershockLeftImg.setImageResource(R.drawable.rollovershock_right_on);
        rollovershockLeftText.setTextColor(getResources().getColor(R.color.green_back));
        rollovershockLeftNumText.setTextColor(getResources().getColor(R.color.green_back));
        rollovershockLeftNum.setTextColor(getResources().getColor(R.color.green_back));
        rollovershockLeftLayout.setBackgroundResource(R.drawable.rollovershock_on_item);
    }

    private void pingOn() {
        rollovershockPingImg.setImageResource(R.drawable.rollovershock_ping_on);
        rollovershockPingText.setTextColor(getResources().getColor(R.color.green_back));
        rollovershockPingLayout.setBackgroundResource(R.drawable.rollovershock_on_item);
    }

    private void RightOn() {

        rollovershockRightImg.setImageResource(R.drawable.rollovershock_left_on);
        rollovershockRightText.setTextColor(getResources().getColor(R.color.green_back));
        rollovershockRightNumText.setTextColor(getResources().getColor(R.color.green_back));
        rollovershockRightNum.setTextColor(getResources().getColor(R.color.green_back));
        rollovershockRightLayout.setBackgroundResource(R.drawable.rollovershock_on_item);
    }

    private void QZAllOff() {
        rollovershockQiaojiImg.setImageResource(R.drawable.rollovershock_qiaoji_off);
        rollovershockQiaojiTextone.setTextColor(getResources().getColor(R.color.white));
        rollovershockQiaojiTexttwo.setTextColor(getResources().getColor(R.color.white));
        rollovershockQiaojiTextthree.setTextColor(getResources().getColor(R.color.white));
        rollovershockQiaojiTextfour.setTextColor(getResources().getColor(R.color.white));
        rollovershockQiaojiTextfive.setTextColor(getResources().getColor(R.color.white));
        rollovershockQiaojiTextsix.setTextColor(getResources().getColor(R.color.white));
        rollovershockQiaojiQiangdu.setTextColor(getResources().getColor(R.color.white));
        rollovershockRightTwoLayout.setBackgroundResource(R.drawable.rollovershock_off_item);
        rollovershockZhendongImg.setImageResource(R.drawable.rollovershock_zhendong_off);
        rollovershockZhendongTextone.setTextColor(getResources().getColor(R.color.white));
        rollovershockZhendongTexttwo.setTextColor(getResources().getColor(R.color.white));
        rollovershockZhendongTextthree.setTextColor(getResources().getColor(R.color.white));
        rollovershockZhendongTextfour.setTextColor(getResources().getColor(R.color.white));
        rollovershockZhendongTextfive.setTextColor(getResources().getColor(R.color.white));
        rollovershockZhendongTextsix.setTextColor(getResources().getColor(R.color.white));
        rollovershockZhendongQiangdu.setTextColor(getResources().getColor(R.color.white));
        rollovershockRightThreeLayout.setBackgroundResource(R.drawable.rollovershock_off_item);
    }

    private void QiaojiOn() {
        rollovershockQiaojiQiangdu.setTextColor(getResources().getColor(R.color.green_back));
        rollovershockQiaojiImg.setImageResource(R.drawable.rollovershock_qiaoji_on);
        rollovershockQiaojiTextone.setTextColor(getResources().getColor(R.color.green_back));
        rollovershockQiaojiTexttwo.setTextColor(getResources().getColor(R.color.green_back));
        rollovershockQiaojiTextthree.setTextColor(getResources().getColor(R.color.green_back));
        rollovershockQiaojiTextfour.setTextColor(getResources().getColor(R.color.green_back));
        rollovershockQiaojiTextfive.setTextColor(getResources().getColor(R.color.green_back));
        rollovershockRightTwoLayout.setBackgroundResource(R.drawable.rollovershock_on_item);
        rollovershockQiaojiTextsix.setTextColor(getResources().getColor(R.color.green_back));
    }

    private void ZhendongOn() {
        rollovershockZhendongQiangdu.setTextColor(getResources().getColor(R.color.green_back));
        rollovershockZhendongImg.setImageResource(R.drawable.rollovershock_zhendong_on);
        rollovershockZhendongTextone.setTextColor(getResources().getColor(R.color.green_back));
        rollovershockZhendongTexttwo.setTextColor(getResources().getColor(R.color.green_back));
        rollovershockZhendongTextthree.setTextColor(getResources().getColor(R.color.green_back));
        rollovershockZhendongTextfour.setTextColor(getResources().getColor(R.color.green_back));
        rollovershockZhendongTextfive.setTextColor(getResources().getColor(R.color.green_back));
        rollovershockRightThreeLayout.setBackgroundResource(R.drawable.rollovershock_on_item);
        rollovershockZhendongTextsix.setTextColor(getResources().getColor(R.color.green_back));
    }

    private void mTimer() {
        // 设置24时制显示格式
        rollovershockTimeNum.setText("00:00:00");
        rollovershockRightTimeNum.setText("00:00:00");
        stepTimeHandler = new Handler();
        startTime = System.currentTimeMillis();
        mTicker = new Runnable() {
            public void run() {
                String content = TestTime.showTimeCount(System.currentTimeMillis() - startTime);
                rollovershockTimeNum.setText(content);
                rollovershockRightTimeNum.setText(content);
                long now = SystemClock.uptimeMillis();
                long next = now + (1000 - now % 1000);
                stepTimeHandler.postAtTime(mTicker, next);
            }
        };
        //启动计时线程，定时更新
        mTicker.run();
    }

    /**
     * 敲击震动带侧翻功能
     *
     * @param time
     * @param qjbps
     * @param zdbps
     * @param qjtime
     * @param zdtime
     */
    private void QJCF(long time, String qjbps, String zdbps, String qjtime, String zdtime, int timerType) {
        loopTime = time;
        rollovershockQiaojiTextthree.setText(qjbps);
        rollovershockQiaojiTextfive.setText(qjtime);
        rollovershockZhendongTextthree.setText(zdbps);
        rollovershockZhendongTextfive.setText(zdtime);
        if (timerType == 4) {
            if (CommandUtil.SHOCK_QIAOJI_STRENGTH == 1) {
                whatRunning = 2;
                zhendongShockMth();
            } else if (CommandUtil.SHOCK_ZHENDONG_STRENGTH == 1) {
                whatRunning = 1;
                qiaojiShockMth();
            } else {
                //每隔一秒钟检测一次
                qiaojiShockMth();
                IoTimer.shareTimer().scheduleTimer(1000, new Runnable() {
                    @Override
                    public void run() {
                        if (!isQJCFTrue) {
                            isQJCFTrue = true;
                            mShockTime();
                        }
                    }
                }, 3, 0);
            }

        } else {
            LoopTimer(loopTime, 2, timerType);
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                switch (CommandUtil.SHOCK_POSITION_TYPE) {
                    case 0:
                        rollovershockCefanTexttwo.setText(R.string.rollover_toping);
                        pingOn();
                        pingRolloverMth();
                        Log.d("yangshuo", "敲击震动-平卧!!!!!!!!!!");
                        break;
                    case 1:
                        rollovershockLeftNum.setText(SPUtils.getInstance().getInt("rollover_left_bfb") * 10 + "%");
                        rollovershockRightNum.setText("--%");
//                        rollovershockRightNum.setText(SPUtils.getInstance().getInt("rollover_right_bfb") * 10 + "%");
                        leftRolloverMth();
                        rollovershockCefanTexttwo.setText(R.string.rollover_toleft);
                        Log.d("yangshuo", "敲击震动-左侧卧!!!!!!!!!!");
                        break;
                    case 2:
                        rightRolloverMth();
//                      rollovershockLeftNum.setText(SPUtils.getInstance().getInt("rollover_left_bfb") * 10 + "%");
                        rollovershockLeftNum.setText("--%");
                        rollovershockRightNum.setText(SPUtils.getInstance().getInt("rollover_right_bfb") * 10 + "%");
                        rollovershockCefanTexttwo.setText(R.string.rollover_toright);
                        Log.d("yangshuo", "敲击震动-右侧卧!!!!!!!!!!");
                        break;
                    case 3:
                        rollovershockCefanTexttwo.setText(R.string.rollover_high);
                        LoopFixedTimer(CommandUtil.THREE_MINUTE, 100000, 1);
                        rollovershockLeftNum.setText("100%");
                        rollovershockRightNum.setText("100%");
                        Log.d("yangshuo", "敲击震动-侧翻整装!!!!!!!!!!");
                        break;
                    case 4:
                        rollovershockLeftNum.setText("80%");
                        rollovershockRightNum.setText("80%");
                        rollovershockCefanTexttwo.setText(R.string.rollover_secondary);
                        LoopFixedTimer(CommandUtil.THREE_MINUTE, 100000, 2);
                        Log.d("yangshuo", "敲击震动-侧翻中等!!!!!!!!!!");
                        break;
                    case 5:
                        rollovershockLeftNum.setText("60%");
                        rollovershockRightNum.setText("60%");
                        rollovershockCefanTexttwo.setText(R.string.rollover_low);
                        LoopFixedTimer(CommandUtil.THREE_MINUTE, 100000, 3);
                        Log.d("yangshuo", "敲击震动-侧翻最低!!!!!!!!!!");
                        break;
                    case 6:
                        Log.d("yangshuo", "敲击震动-侧翻定制!!!!!!!!!!");
                        rollovershockLeftNum.setText(SPUtils.getInstance().getInt("rollover_left_bfb") * 10 + "%");
                        rollovershockRightNum.setText(SPUtils.getInstance().getInt("rollover_right_bfb") * 10 + "%");
                        rightRolloverMth();
                        //每隔一秒钟检测一次
                        IoTimer.shareTimer().scheduleTimer(1000, new Runnable() {
                            @Override
                            public void run() {
                                if (!isCFTrue) {
                                    isCFTrue = true;
                                    mRolloverTime();
                                }
                            }
                        }, 1000000, 300);
                        break;
                }
            }
        });

    }

    /**
     * 敲击震动定时器
     *
     * @param mLoopTime
     * @param frequency
     * @param Type      1:高，2：中，3：低
     */
    private void LoopTimer(long mLoopTime, int frequency, int Type) {
        Log.d("yangshuoTimer", mLoopTime + "");
        IoTimer.shareTimer().scheduleTimer(mLoopTime, new Runnable() {
            @Override
            public void run() {
                isQZTimer = true;
                switch (mShockNowType) {
                    case 1:
                        switch (Type) {
                            case 1:
                                SerialProtUtil.getInstance().sendBytes500(2, CommandUtil.SHOCK_HIGH_QJ);
                                break;
                            case 2:
                                SerialProtUtil.getInstance().sendBytes500(2, CommandUtil.SHOCK_SECONDARY_QJ);
                                break;
                            case 3:
                                SerialProtUtil.getInstance().sendBytes500(2, CommandUtil.SHOCK_LOW_QJ);
                                break;
                        }
                        mShockNowType = 2;
                        break;
                    case 2:
                        switch (Type) {
                            case 1:
                                SerialProtUtil.getInstance().sendBytes500(2, CommandUtil.SHOCK_HIGH_ZD);
                                break;
                            case 2:
                                SerialProtUtil.getInstance().sendBytes500(2, CommandUtil.SHOCK_SECONDARY_ZD);
                                break;
                            case 3:
                                SerialProtUtil.getInstance().sendBytes500(2, CommandUtil.SHOCK_LOW_ZD);
                                break;
                        }
                        break;
                }
            }
        }, frequency, 0);
    }

    /**
     * 侧翻功能定时器
     *
     * @param mLoopTime 循环时间
     * @param frequency 循环次数
     * @param mType     循环类型1：整装，2：一般，3：最低
     */
    private void LoopFixedTimer(long mLoopTime, int frequency, int mType) {

        IoTimer.shareTimer().scheduleTimer(mLoopTime, new Runnable() {
            @Override
            public void run() {
                isCFTimer = true;
                switch (mType) {
                    case 1:
                        switch (mRestType) {
                            case 1:
                                SerialProtUtil.getInstance().sendBytes(2, CommandUtil.CEFAN_HIGH_RIGHT);
                                mRestType = 2;
                                break;
                            case 2:
                                SerialProtUtil.getInstance().sendBytes(2, CommandUtil.CEFAN_HIGH_PING);
                                if (nowCFtype) {
                                    mRestType = 3;
                                    nowCFtype = false;
                                } else {
                                    mRestType = 1;
                                    nowCFtype = true;
                                }
                                break;
                            case 3:
                                SerialProtUtil.getInstance().sendBytes(2, CommandUtil.CEFAN_HIGH_LEFT);
                                mRestType = 2;
                                break;
                        }
                        break;
                    case 2:
                        switch (mRestType) {
                            case 1:
                                SerialProtUtil.getInstance().sendBytes(2, CommandUtil.CEFAN_SECONDARY_RIGHT);
                                mRestType = 2;
                                break;
                            case 2:
                                SerialProtUtil.getInstance().sendBytes(2, CommandUtil.CEFAN_SECONDARY_PING);
                                if (nowCFtype) {
                                    mRestType = 3;
                                    nowCFtype = false;
                                } else {
                                    mRestType = 1;
                                    nowCFtype = true;
                                }
                                break;
                            case 3:
                                SerialProtUtil.getInstance().sendBytes(2, CommandUtil.CEFAN_SECONDARY_LEFT);
                                mRestType = 2;
                                break;
                        }
                        break;
                    case 3:
                        switch (mRestType) {
                            case 1:
                                SerialProtUtil.getInstance().sendBytes(2, CommandUtil.CEFAN_LOW_RIGHT);
                                mRestType = 2;
                                break;
                            case 2:
                                SerialProtUtil.getInstance().sendBytes(2, CommandUtil.CEFAN_LOW_PING);
                                if (nowCFtype) {
                                    mRestType = 3;
                                    nowCFtype = false;
                                } else {
                                    mRestType = 1;
                                    nowCFtype = true;
                                }
                                break;
                            case 3:
                                SerialProtUtil.getInstance().sendBytes(2, CommandUtil.CEFAN_LOW_LEFT);
                                mRestType = 2;
                                break;
                        }
                        break;
                }
            }
        }, frequency, 0);
    }

    /**
     * 右翻定制
     */
    private void rightRolloverMth() {
        byte[] mRightSendBytes = CommandUtil.CEFAN_CUNTOMIZED_RIGHT;
        mRightSendBytes[8] = (byte) SPUtils.getInstance().getInt("rollover_right_bfb");
        SerialProtUtil.getInstance().sendBytes(2, mRightSendBytes);
        mRolloverNowType = 2;
    }

    /**
     * 平卧定制
     */
    private void pingRolloverMth() {
        byte[] mPingSendTime = CommandUtil.CEFAN_CUNTOMIZED_PING;
        SerialProtUtil.getInstance().sendBytes(2, mPingSendTime);
        mRolloverNowType = 3;
    }

    /**
     * 左翻定制
     */
    private void leftRolloverMth() {
        byte[] mPingSendTime = CommandUtil.CEFAN_CUNTOMIZED_LEFT;
        mPingSendTime[8] = (byte) SPUtils.getInstance().getInt("rollover_left_bfb");
        SerialProtUtil.getInstance().sendBytes(2, mPingSendTime);
        mRolloverNowType = 1;
    }

    /**
     * 敲击定制
     */
    private void qiaojiShockMth() {
        byte[] mQiaojiSendByte = CommandUtil.SHOCK_ZDY_QJ;
        mQiaojiSendByte[8] = (byte) CommandUtil.SHOCK_QIAOJI_BPS;
        mQiaojiSendByte[9] = (byte) CommandUtil.SHOCK_QIAOJI_STRENGTH;
        SerialProtUtil.getInstance().sendBytes200(2, mQiaojiSendByte);
        mShockNowType = 2;
    }

    /**
     * 震动定制
     */
    private void zhendongShockMth() {
        byte[] mZhendongSendByte = CommandUtil.SHOCK_ZDY_ZD;
        mZhendongSendByte[8] = (byte) CommandUtil.SHOCK_ZHENDONG_BPS;
        mZhendongSendByte[9] = (byte) CommandUtil.SHOCK_ZHENDONG_STRENGTH;
        SerialProtUtil.getInstance().sendBytes200(2, mZhendongSendByte);
        mShockNowType = 3;
    }

    private void mRolloverTime() {
        /**
         * CountDownTimer 实现倒计时
         */
        countDownTimer = new CountDownTimer(mRolloverNowTime, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
                switch (mRolloverNowType) {
                    case 1:
                        Log.d("yangshuo", "RIGHT____CLEAR!!!!!!!!!!");
                        mRolloverNowTime = SPUtils.getInstance().getInt("rollover_right_minute") * 60000;
                        mRolloverNowType = 2;
                        rightRolloverMth();
                        break;
                    case 2:
                        Log.d("yangshuo", "PING____CLEAR!!!!!!!!!!");
                        mRolloverNowTime = SPUtils.getInstance().getInt("rollover_ping_minute") * 60000;
                        mRolloverNowType = 3;
                        pingRolloverMth();
                        break;
                    case 3:
                        Log.d("yangshuo", "LEFT____CLEAR!!!!!!!!!!");
                        mRolloverNowTime = SPUtils.getInstance().getInt("rollover_left_minute") * 60000;
                        mRolloverNowType = 1;
                        leftRolloverMth();
                        break;
                }
                isCFTrue = false;
            }
        };
        countDownTimer.start();
    }

    private void mShockTime() {
        /**
         * CountDownTimer 实现倒计时
         */
        countDownTimer = new CountDownTimer(mShockNowTime, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
                switch (mShockNowType) {
                    case 1:
                        Log.d("yangshuo", "QIAOJI____CLEAR!!!!!!!!!!");
                        mShockNowTime = SPUtils.getInstance().getInt("shock_time_qiaoji") * 60000;
                        qiaojiShockMth();
                        mShockNowType = 2;

                        break;
                    case 2:
                        Log.d("yangshuo", "ZHENDONG____CLEAR!!!!!!!!!!");
                        mShockNowTime = SPUtils.getInstance().getInt("shock_time_zhendong") * 60000;
                        zhendongShockMth();
                        mShockNowType = 3;

                        break;

                }
                isQJCFTrue = false;
            }
        };
        countDownTimer.start();
    }

    private Disposable mTimeCountDisposable;
    private int timeLong = 180;

    /**
     * 倒计时
     */
    public void TimeCount() {
        switch (whatRunning) {
            case 0:
                timeLong = SPUtils.getInstance().getInt("shock_time_zhendong", 180) * 60;
                break;
            case 1:
                timeLong = SPUtils.getInstance().getInt("shock_time_qiaoji", 180) * 60;
                break;
            case 2:
                timeLong = SPUtils.getInstance().getInt("shock_time_zhendong", 180) * 60;
                break;
            case 3:
                timeLong = 15 * 60;
                break;
            case 4:
                timeLong = 10 * 60;
                break;
        }
        if (mTimeCountDisposable != null) {
            //取消上一次的订阅
            mTimeCountDisposable.dispose();
        }
//        //0-count依次输出，延时0s执行，每1s发射一次。
        mTimeCountDisposable = Flowable.intervalRange(0, timeLong, 0, 1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(aLong -> {

                })
                .doOnComplete(() -> {
                    if (mTimeCountDisposable != null)
                        mTimeCountDisposable.dispose();
                    needFinish = true;
                    SerialProtUtil.getInstance().sendBytes(2, CommandUtil.CEFAN_STOP);
                })
                .subscribe();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SerialProtUtil.getInstance().sendBytes300(2, CommandUtil.CHANGGUI_TTYPE);
        IoTimer.destroyTimer();
    }

    @OnClick({R.id.rollovershock_stop_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rollovershock_stop_button:
                needFinish = true;
                CommandUtil.IS_ROLLOVEW_SHOCK = false;
                SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.CEFAN_STOP);
                break;
        }
    }
}
